#!/bin/sh

#############################################################################
## 
##   Logtalk Debian package auxiliary script
##   Last updated on December 16, 2024
## 
##   This file is part of Logtalk <https://logtalk.org/>  
##   SPDX-FileCopyrightText: 1998-2025 Paulo Moura <pmoura@logtalk.org>
##   SPDX-License-Identifier: Apache-2.0
##   
##   Licensed under the Apache License, Version 2.0 (the "License");
##   you may not use this file except in compliance with the License.
##   You may obtain a copy of the License at
##   
##       http://www.apache.org/licenses/LICENSE-2.0
##   
##   Unless required by applicable law or agreed to in writing, software
##   distributed under the License is distributed on an "AS IS" BASIS,
##   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
##   See the License for the specific language governing permissions and
##   limitations under the License.
## 
#############################################################################


echo ""
echo "Installed Logtalk on \"/usr/share\"."

chown -f -R -L root:root /usr/share/logtalk

echo ""
echo "Installing and registering Logtalk online documentation..."

if [ "$1" = "configure" ] ; then
	if which install-docs > /dev/null 2>&1 ; then
		install-docs -i /usr/share/doc-base/logtalk-docs
	fi
fi

chown -f -R -L root:root /usr/share/doc/logtalk

echo "Adding menu entries for some of the Logtalk integration scripts..."

if [ -x /usr/bin/update-menus ] ; then
	update-menus
fi

echo "Links to the following scripts have been created in /usr/bin:"
echo
echo "- logtalk_user_setup, logtalk_backend_select, logtalk_version_select"
echo "- logtalk_tester, logtalk_allure_report, logtalk_doclet"
echo "- lgt2svg, lgt2pdf, lgt2html, lgt2xml, lgt2md, lgt2rst, lgt2txt"
echo "- lgtenv"
echo
echo "The following integration scripts are installed for running Logtalk"
echo "with selected backend Prolog compilers:"
echo
echo "* B-Prolog (8.1 or later):           bplgt       (experimental)"
echo "* Ciao Prolog (1.22.0 or later):     ciaolgt     (experimental; first run may require sudo)"
echo "* CxProlog (0.98.1 or later):        cxlgt"
echo "* ECLiPSe (6.1#143 or later):        eclipselgt"
echo "* GNU Prolog (1.4.5 or later):       gplgt"
echo "* JIProlog (4.1.7.1 or later):       jiplgt      (first run may require sudo)"
echo "* Quintus Prolog (3.3 or later):     quintuslgt  (experimental)"
echo "* SICStus Prolog (4.1.0 or later):   sicstuslgt"
echo "* SWI-Prolog (6.6.0 or later):       swilgt"
echo "* Tau Prolog (0.3.2 or later):       taulgt"
echo "* Trealla Prolog (2.59.21 or later): tplgt"
echo "* XSB (3.8.0 or later):              xsblgt      (first run may require sudo)"
echo "* XVM (10.0.0 or later):             xvmlgt"
echo "* YAP (6.3.4 or later):              yaplgt"
echo
echo "Links to the Prolog integration scripts can be found on \"/usr/bin\"."
echo "Make sure that the Prolog compilers are properly installed and available"
echo "on your execution path."
echo
echo "Users should run the \"logtalk_user_setup\" shell script once before using"
echo "the integration scripts."
echo
echo "If you get an unexpected failure when using one of the Prolog integration"
echo "scripts, consult see the \"/usr/share/logtalk/adapters/NOTES.md\" file"
echo "for compatibility notes or consult the integration script man page."

echo
mkdir -p /etc/profile.d
echo "# Logtalk environment setup" > /etc/profile.d/logtalk.sh
echo "" >> /etc/profile.d/logtalk.sh
echo "# Logtalk installation directory:" >> /etc/profile.d/logtalk.sh
echo "export LOGTALKHOME=/usr/share/logtalk" >> /etc/profile.d/logtalk.sh
echo "" >> /etc/profile.d/logtalk.sh
echo "# Default location for Logtalk end-user files:" >> /etc/profile.d/logtalk.sh
echo "export LOGTALKUSER=\$HOME/logtalk" >> /etc/profile.d/logtalk.sh
chmod a+x /etc/profile.d/logtalk.sh
echo "# Logtalk environment setup" > /etc/profile.d/logtalk.csh
echo "" >> /etc/profile.d/logtalk.csh
echo "# Logtalk installation directory:" >> /etc/profile.d/logtalk.csh
echo "setenv LOGTALKHOME /usr/share/logtalk" >> /etc/profile.d/logtalk.csh
echo "" >> /etc/profile.d/logtalk.csh
echo "# Default location for Logtalk end-user files:" >> /etc/profile.d/logtalk.csh
echo "setenv LOGTALKUSER \$HOME/logtalk" >> /etc/profile.d/logtalk.csh
chmod a+x /etc/profile.d/logtalk.csh
echo "Defined the following environment variables for all users:"
echo
echo "  Logtalk installation directory: LOGTALKHOME = /usr/share/logtalk"
echo "  Default Logtalk user files directory: LOGTALKUSER = \$HOME/logtalk"
echo
echo "You may need to logout and login again or start a new shell in order to"
echo "use the new environment variables."
echo
echo "Users may change the default value of the LOGTALKUSER environment variable"
echo "in their shell configuration files if they already use, or want to use, a "
echo "different location for the Logtalk user files directory. This directory "
echo "is created by running the \"logtalk_user_setup\" shell script, which must"
echo " be run once by each user before using the integration scripts."

if [ -x /usr/bin/update-mime-database ] ; then
	mkdir -p /usr/share/mime/packages
	rm -f /usr/share/mime/packages/logtalk.xml
	cp /usr/share/logtalk/scripts/freedesktop/logtalk.xml /usr/share/mime/packages/logtalk.xml
	update-mime-database /usr/share/mime
	echo "Added the Logtalk mime-type to the Shared MIME-info Database."
	echo
fi

echo "Logtalk basic installation completed."
echo
exit 0
